package com.wuzf.mall.bottom

import android.graphics.Color
import android.os.Bundle
import android.support.annotation.ColorInt
import android.support.v7.widget.AppCompatTextView
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.joanzapata.iconify.widget.IconTextView
import com.wuzf.mall.R
import com.wuzf.mall.fragment.MallFragment

abstract class BaseBottomFragment : MallFragment(), View.OnClickListener {

    private var mItems = LinkedHashMap<BottomTabBean, BottomTabFragment>()
    private var mTabs = ArrayList<BottomTabBean>()
    private var mFragments = ArrayList<BottomTabFragment>()
    private val mLinearLayoutList = ArrayList<LinearLayout>()

    // 设置第几位
    private var mIndex = 0

    private var mColor = 0


    abstract fun setIndex(): Int

    @ColorInt
    abstract fun setColorInt(): Int

    abstract fun setItems(): ItemBuilder

    override fun setLayout(): Any {
        return R.layout.base_activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 数据进行初始化
        mIndex = setIndex()
        mColor = setColorInt()
        mItems = setItems().build()
        for ((key, value) in mItems) {
            mTabs.add(key)
            mFragments.add(value)
        }
    }

    override fun onBindView(savedInstanceState: Bundle?, rootView: View) {
        var bottomLayout = findView(R.id.main_bottom) as LinearLayout
        for (i in 0 until mTabs.size) {
            LayoutInflater.from(context).inflate(R.layout.bottom_item_layout, bottomLayout)
            val childLayout = bottomLayout.getChildAt(i) as LinearLayout
            childLayout.tag = i
            mLinearLayoutList.add(childLayout)
            childLayout.setOnClickListener(this)
            var iconText = childLayout.getChildAt(0) as IconTextView
            var title = childLayout.getChildAt(1) as AppCompatTextView
            iconText.text = mTabs[i].icon
            title.text = mTabs[i].title
            if (i == mIndex) {
                iconText.setTextColor(mColor)
                title.setTextColor(mColor)
            }
        }
        val toArray = mFragments.toTypedArray()
        supportDelegate.loadMultipleRootFragment(R.id.main_content, mIndex, *toArray)
    }

    override fun onClick(v: View?) {
        // 点击进行跳转
        val index = v?.tag as Int
        clearColors()
        supportDelegate.showHideFragment(mFragments[index], mFragments[mIndex])
        addColor(index)
        mIndex = index
    }

    private fun clearColors() {
        for (childLayout in mLinearLayoutList) {
            val iconText = childLayout.getChildAt(0) as IconTextView
            val title = childLayout.getChildAt(1) as AppCompatTextView
            iconText.setTextColor(Color.BLACK)
            title.setTextColor(Color.BLACK)
        }
    }

    private fun addColor(index: Int) {
        val iconText = mLinearLayoutList[index].getChildAt(0) as IconTextView
        val title = mLinearLayoutList[index].getChildAt(1) as AppCompatTextView
        iconText.setTextColor(mColor)
        title.setTextColor(mColor)
    }
}